package com.example.web;

import com.example.model.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class Import {

    /**
     * list列表输出
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String eachList(Model model){
        List<Student> studentList = new ArrayList<Student>();
        for (int i = 0;i < 10;i++){
            Student student = new Student();
            student.setId(100 + i);
            student.setNick("张" + i);
            student.setPhone("1361234567" + i);
            student.setAddress("北京市大兴区" + i);
            studentList.add(student);
        }
        model.addAttribute("studentList",studentList);
        return "eachList";
    }

    /**
     * map集合
     * @param model
     * @return解决  String类型 需要传参问题  内置Model的形式参
     */
    @RequestMapping(value = "/map")
    public String eachMap(Model model) {
        Map<Integer, Object> studentMaps = new HashMap<Integer, Object>();

        for (int i = 0; i < 10; i++) {
            Student student = new Student();
            student.setId(i);
            student.setNick("李四" + i);
            student.setPhone("1390000000" + i);
            student.setAddress("天津市" + i);
            studentMaps.put(i, student);
        }
        model.addAttribute("studentMaps", studentMaps);
        return "eachMap";
    }

    /**
     * 数组 大致同List
     * @param model
     * @return
     */
    @RequestMapping(value = "/each/array")
    public String eachArray(Model model) {

        Student[] studentArray = new Student[10];

        for (int i = 0; i < 10; i++) {

            Student student = new Student();
            student.setId(i);
            student.setNick("赵六" + i);
            student.setPhone("1380000000" + i);
            student.setAddress("深圳市" + i);
            studentArray[i] = student;

        }
        model.addAttribute("studentArray", studentArray);

        model.addAttribute("inner", "c");

        return "eachArray";
    }

    @RequestMapping(value = "/each/all")
    public String eachAll(Model model) {
        //list -> Map -> List -> Student
        List<Map<Integer, List<Student>>> myList = new ArrayList<Map<Integer, List<Student>>>();

        for (int i = 0; i < 2; i++) {

            Map<Integer, List<Student>> myMap = new HashMap<Integer, List<Student>>();
            for (int j = 0; j < 2; j++) {
                List<Student> myStudentList = new ArrayList<Student>();
                for (int k = 0; k < 3; k++) {
                    Student student = new Student();
                    student.setId(k);
                    student.setNick("张三" + k);
                    student.setPhone("1350000000" + k);
                    student.setAddress("广州市" + i);
                    myStudentList.add(student);
                }
                myMap.put(j, myStudentList);
            }
            myList.add(myMap);

        }
        model.addAttribute("myList", myList);
        return "eachAll";
    }



}
